<?php
	
	include_once 'header.php';
	//如果是未登陆则跳转到登陆页
	if($uid <= 0 ){
		echo '<script type="text/javascript">location.href = \'login.php\';</script>';
		die(0);
	}
	//查询购物车商品列表
	$stmt = $pdo->prepare('select a.gid,a.checked,a.num carnum,a.newprice,b.gname,b.gphoto,b.zkprice,b.num from (select gid,checked,num,newprice,newdate from kp_carlist where uid=? order by newdate desc) a left join kp_goods b on a.gid = b.gid');
	$stmt->execute(array($uid));
	$goodArr = $stmt->fetchAll(PDO::FETCH_ASSOC);//得到购物车商品数组
	//print_r($goodArr);
	
?>
<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<meta http-equiv="X-UA-Compatible" content="ie=edge" />
		<title>Document</title>
		<link rel="stylesheet" type="text/css" href="css/carlist.css"/>
		<script src="js/jquery-3.1.1.js" type="text/javascript" charset="utf-8"></script>
		<!--<script src="http://code.jquery.com/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>-->
		
		<script src="js/carlist.js" type="text/javascript" charset="utf-8"></script>
		
	</head>
	<body>
		<div id="carlist">
			<div class="goods">
				<div class="title">
					<h2>您的位置: 购物车</h2>
				</div>
				<!--购物车商品标题行-->
				<ul class="gtitle">
					<li class="col0"><label><input type="checkbox" id='checkall' value="all" />全选</label></li>
					<li class="col1">商品标题</li>
					<li class="col2">单价</li>
					<li class="col3">数量</li>
					<li class="col4">小计</li>
					<li class="col5">操作</li>
				</ul>
				<!--购物车商品start-->
				
				<?php
				$sum = 0;
				$allzt = true;//所有商品选中状态
				foreach($goodArr as $good){
					$gphotos = [];
						if(!empty($good['gphoto'])){
							$gphotos = explode(';', $good['gphoto']);
						}
				?>
				<form id="form1" action="getorder.php" method="post">
					<ul class="good" id="<?php echo 'good_'.$good['gid']; ?>">
					
						<!--选择+图片-->
						<li class="col0">
							<input name="gid[]" type="checkbox" <?php if($good['checked'] == 1) echo 'checked="checked"';  ?> id="<?php echo 'g_'.$good['gid']; ?>" value="<?php echo $good['gid']; ?>" />
							<img class="goodimg" src="<?php if(!empty($gphotos[0])) echo  $gphotos[0]; else echo 'upload/good_001.gif'; ?>"/>
						</li>
						<!--标题-->
						<li class="col1" title="<?php echo $good['gname']; ?>"><?php echo $good['gname']; ?></li>
						<!--单价-->
						<li class="col2"><span id="">¥<span id="<?php echo 'p_'.$good['gid']; ?>" class="nowprice"><?php echo $good['zkprice']; ?></span><br /><span class="cajia">
							<?php 
								if(($good['zkprice'] - $good['newprice']) > 0){
									echo '涨:'.number_format(($good['zkprice'] - $good['newprice']),2,'.','');	
								}
								elseif(($good['zkprice'] - $good['newprice']) < 0){
									echo '降:'.number_format(($good['newprice'] - $good['zkprice']),2,'.','');
								}
						 	?></span></li>
						 	<!--数量-->
						<li class="col3">
							<input type="button" id="<?php echo 'u_'.$good['gid']; ?>" value="-" onclick="minus(<?php echo $good['gid']; ?>,this);" />
							<input type="text"  class="num" id="<?php echo 'n_'.$good['gid']; ?>" value="<?php echo (int)$good['carnum']; ?>" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onblur = "numblur(<?php echo $good['gid']; ?>)"/>
							<input type="button" id="<?php echo 'a_'.$good['gid']; ?>" value="+"  onclick="add(<?php echo $good['gid']; ?>,this);" />
							<br />
							<span class="nownum">库存:<span id="<?php echo 'k_'.$good['gid']; ?>"><?php echo (int)$good['num']; ?></span></span>
						</li>
						<!--小计-->
						<li class="col4">¥<span id="<?php echo 's_'.$good['gid']; ?>"><?php echo  number_format(($good['zkprice'] * $good['carnum']),2,'.',''); ?></span></li>
						<!--操作:删除-->
						<li class="col5">
							<input type="button" id="<?php echo 'r_'.$good['gid']; ?>" value="删除" onclick="removeCar(<?php echo $good['gid']; ?>)" />
						</li>
					</ul>
				
				<?php
						//$sum += ($good['zkprice'] * $good['carnum']);//总金额不需要计算,勾选后才需要计算
						//$sum = 0.00;
						if($good['checked'] == 0){
							$allzt = false;
						}else{
							$sum += ($good['zkprice'] * $good['carnum']);//总金额不需要计算,勾选后才需要计算
						}
					}
					//如果全选中,则全选也钩选
					if($allzt){
						echo '<script type="text/javascript">$("#checkall").attr("checked","checked");</script>';
					}
				?>
				
				<!--购物车商品列表end-->
				<ul class="gbottom">
					<li class="col0"><label><!--<input type="checkbox" class="g_all" value="all" />全选-->汇总:</label></li>
					<li class="colb0">&nbsp;</li>
					<li class="col4">总价：¥<span id="money"><?php echo  number_format($sum,2,'.',''); ?></span></li>
					<li class="col5"><input type="submit" id="submit" value="结算" /></li>
				</ul>
				</form>
			</div>
			<!--
				结算:
				1.生成结算单:
				2.删除购物车中结算商品
				跳转到结算页
				
				-->
			
			
			
			
		</div>
		<?php include 'bottom.php' ?>
	</body>
</html>